import { ref } from 'vue'
import { getEnabledCheckFlowTemplate as getEnabledCheckFlowTemplateApi } from '@/api/basic'

/**
 * 获取启用的检测流程模板服务
 */
export default function () {
  const getEnabledCheckFlowTemplateLoading = ref(false)
  const enabledCheckFlowTemplate = ref<BaseInfoAPIResponse.CheckFlowEnabledTemplate>({
    flowTemplateEntity: {
      code: '',
      description: '',
      gmtCreate: '',
      gmtUpdate: '',
      id: '',
      name: '',
    },
    flowTemplateNodeRelationList: [],
  })

  const getEnabledCheckFlowTemplate = async () => {
    try {
      getEnabledCheckFlowTemplateLoading.value = true
      const res = await getEnabledCheckFlowTemplateApi()
      getEnabledCheckFlowTemplateLoading.value = false
      if (res.data.code === 20000) {
        enabledCheckFlowTemplate.value = res.data.data
        return ''
      }

      return res.data.message
    } catch (error) {
      getEnabledCheckFlowTemplateLoading.value = false
      return '获取启用的模板流程失败，请稍后重试'
    }
  }

  return {
    enabledCheckFlowTemplate,
    getEnabledCheckFlowTemplateLoading,
    getEnabledCheckFlowTemplate,
  }
}
